package cn.edu.jlu.community.app.post;


import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.edu.jlu.community.R;
import cn.edu.jlu.community.server.AppServer;
import cn.edu.jlu.community.util.Log;
import cn.edu.jlu.community.util.ToastUtil;


public class CommentFragment extends ListFragment
        implements AppServer.LoaderCallback, AppServer.PushCallback {

    private static final String ARG_ID = "param_id";
    private String mId;

    @InjectView(R.id.loading)
    View loadingView;
    @InjectView(R.id.empty)
    View emptyView;

    @InjectView(R.id.container)
    ViewGroup rootLayout;

    @InjectView(R.id.new_comment_edit)
    EditText commentEdit;

    public static CommentFragment newInstance(String id) {
        CommentFragment fragment = new CommentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_ID, id);
        fragment.setArguments(args);
        return fragment;
    }

    public CommentFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mId = getArguments().getString(ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.post_comment_fragment, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        reload();
    }

    private void reload() {
        if (null != mId) {
            AppServer.loadComment(mId, this);
            getListView().setEmptyView(loadingView);
            emptyView.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.send_comment_button)
    public void sendNewComment() {
        String comment = commentEdit.getText().toString();
        if (!comment.isEmpty()) {
            AppServer.newComment(commentEdit.getText().toString(), mId, this);
            hideKeyBoard();
        }
    }

    @Override
    public void onSuccess() {
        ToastUtil.showText("评论成功");
        AppServer.loadComment(mId, this);
        commentEdit.setText(null);
        hideKeyBoard();
    }

    private void hideKeyBoard() {
        Activity activity = getActivity();
        View view = getView();
        if (null != view) {
            ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onFinish(Object data) {
        List<AVObject> commentList = (List<AVObject>) data;

        if (commentList.size() > 0) {
            setListAdapter(new CommentAdapter(getActivity(), commentList));
        } else {
            emptyView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reload();
                }
            });

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    getListView().setEmptyView(emptyView);
                    loadingView.setVisibility(View.GONE);
                }
            }, 500);
        }
    }

    @Override
    public void onError(AVException e) {

    }

}
